//package com.example.bankmanage.interceptor;
//
//import com.example.bankmanage.Common.UserContext;
//import com.example.bankmanage.Common.UserInfoDTO;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//
///**
// * 用户上下文拦截器，从请求头或 Token 解析用户信息并存入 ThreadLocal
// */
//@WebFilter("/user/login")
//@Component
//@SuppressWarnings("all")
//public class UserContextInterceptor implements HandlerInterceptor {
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//        System.out.println("-------" + request);
//        // 示例：从请求头获取用户信息（实际业务可能从 Token 解析）
//        String userId = request.getHeader("userId");
//        String username = request.getHeader("username");
//
//        if (userId != null && username != null) {
//            UserInfoDTO user = UserInfoDTO.builder()
//                    .userId(Integer.parseInt(userId))
//                    .username(username)
//                    .build();
//            UserContext.setUser(user); // 存入 ThreadLocal
//        }
//
//        return true;
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//        // 请求结束后清理 ThreadLocal，防止内存泄漏
//        UserContext.removeUser();
//    }
//}